package com.cloud.magic_api.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cloud.common.domain.Result;
import com.cloud.common.enums.DeveloperState;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.magic_api.entity.SysUserInfo;
import com.cloud.magic_api.service.IUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 开发者账户信息
 *
 * @author yzj
 * @since 2022-08-26
 */
@Tag(name = "开发账户管理")
@RestController
@RequestMapping("/magicApi/sysUser")
public class SysUserInfoController extends BaseController {

    @Autowired
    private IUserInfoService userInfoService;

    @Operation(summary = "开发账户信息分页")
    @GetMapping("/list")
    public Result list(SysUserInfo sysDeveloper) {
        startPage();
        List<SysUserInfo> list = userInfoService.selectUserList(sysDeveloper);
        return getDataTable(list);
    }

    @Operation(summary = "新增开发账户信息")
    @Logs(title = "新增开发者账户信息", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public Result insert(@RequestBody SysUserInfo sysDeveloper) {
        return userInfoService.insertObject(sysDeveloper);
    }

    @Operation(summary = "修改开发账户信息")
    @Logs(title = "修改开发账户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public Result update(@RequestBody SysUserInfo sysDeveloper) {
        return userInfoService.updateObject(sysDeveloper);
    }

    @Operation(summary = "查询开发账户详情")
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        return Result.success("查询成功", userInfoService.getById(id));
    }

    @Operation(summary = "删除开发账户信息")
    @Logs(title = "删除开发者账户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Result remove(String ids) {
        List<String> list = Arrays.asList(ids.split(StringPool.COMMA));
        return toResult(userInfoService.removeByIds(list));
    }

    @Operation(summary = "启动开发账户状态")
    @Logs(title = "开启开发账户状态", businessType = BusinessType.UPDATE)
    @PostMapping("/startUser")
    public Result startUser(Long userId) {
        return toResult(userInfoService.updateUserState(DeveloperState.ENABLE.getCode(), userId));
    }

    @Operation(summary = "关闭开发账户状态")
    @Logs(title = "关闭开发账户状态", businessType = BusinessType.UPDATE)
    @PostMapping("/closeUser")
    public Result closeUser(Long userId) {
        return toResult(userInfoService.updateUserState(DeveloperState.DISABLE.getCode(), userId));
    }

}
